package com.qf.film.view.controller;

import com.qf.film.view.pojo.FilmInfo;
import com.qf.film.view.service.IFilmService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author King_long
 * @program: 20191025
 * @Date 2019-10-25 15:47
 */
@Controller

public class FilmController {

    private static final Logger LOGGER = LogManager.getLogger(FilmController.class);

    @Autowired
    private IFilmService filmService;


    @RequestMapping("/add")
    public String add(FilmInfo filmInfo, Model model) {

        boolean result = filmService.insert(filmInfo);

        LOGGER.info("================================" + filmInfo);

        if (result) {
            model.addAttribute("msg", "添加成功");
        } else {
            model.addAttribute("msg", "添加失败，请重新添加");
        }
        return "success";
    }

    @RequestMapping("/fuzzysearch")
    public String fuzzySearch(FilmInfo filmInfo, String upper, String lower, Model model) {
        //如果为空  赋值为-1
        int upperNum = -1;
        int lowerNum = -1;
        if (upper!="") {
            upperNum = Integer.parseInt(upper);
        }
        if (lower!=""){
            lowerNum = Integer.parseInt(lower);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("filmInfo", filmInfo);
        map.put("upperNum", upperNum);
        map.put("lowerNum", lowerNum);
        List<FilmInfo> filmInfos = filmService.search(map);
        LOGGER.info("================================" + filmInfos);
        model.addAttribute("filmInfos", filmInfos);
        return "show";
    }

}
